package com.psa.tool;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import com.psa.dialog.DialogManager;
import com.psa.system.R;
import com.wxw.BaseLibrary.element.JsonObject;
import com.wxw.BaseLibrary.element.JsonProperty;
import com.wxw.BaseLibrary.element.JsonPropertyType;

/**
 * Created by david on 2017-08-15.
 */
public class AlertDialogTool {
    public static void showDeleteOrderDialog(Activity activity, String orderID, DialogInterface.OnClickListener DeleteOrderListener){
        show2ButtonAlertDialog(activity, activity.getString(R.string.cust_action_delorder_dialog_title),
                activity.getString(R.string.cust_action_delorder_dialog_text1)+orderID+ activity.getString(R.string.cust_action_delorder_dialog_text2),
                DeleteOrderListener);
    }

    public static void showOneButtonAlertDialog(Context context, String title, String msg, String btnText, final DialogInterface.OnClickListener listener) {
        if (context instanceof Activity && !((Activity)context).isFinishing()) {
            final AlertDialog dialog = new AlertDialog.Builder(context).create();
            dialog.show();
            dialog.setCancelable(false);
            Window window = dialog.getWindow();
            window.setContentView(R.layout.alert_dialog_layout);
            TextView tv_title = (TextView) window.findViewById(R.id.tv_title);
            TextView tv_text = (TextView) window.findViewById(R.id.tv_text);
            Button btn_right = (Button) window.findViewById(R.id.btn_right);
            tv_title.setText(title);
            tv_text.setText(msg);
            btn_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.hide();
                    if (listener != null) {
                        listener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                    }
                }
            });
            btn_right.setText(btnText);
            ((Button) window.findViewById(R.id.btn_left)).setVisibility(View.INVISIBLE);
        }
    }

    public static void show2ButtonAlertDialog(Context context, String title, String msg, final DialogInterface.OnClickListener listener) {
        JsonProperty btnParams = new JsonProperty();
        JsonObject bp = new JsonObject();
        bp.setProperty("id", new JsonProperty(R.id.btn_left));
        bp.setProperty("action", new JsonProperty(Dialog.BUTTON_POSITIVE));
        bp.setProperty("text", new JsonProperty(context.getString(R.string.btn_ok)+""));
        btnParams.add(bp);
        bp = new JsonObject();
        bp.setProperty("id", new JsonProperty(R.id.btn_right));
        bp.setProperty("action", new JsonProperty(Dialog.BUTTON_NEGATIVE));
        bp.setProperty("text", new JsonProperty(context.getString(R.string.btn_cancel)+""));
        btnParams.add(bp);
        showAlertDialog(context, title, msg, btnParams, listener);
    }

    public static void show3ButtonAlertDialog(Context context, String title, String msg, final DialogInterface.OnClickListener listener) {
        JsonProperty btnParams = new JsonProperty();
        JsonObject bp = new JsonObject();
        bp.setProperty("id", new JsonProperty(R.id.btn_left));
        bp.setProperty("action", new JsonProperty(Dialog.BUTTON_NEUTRAL));
        bp.setProperty("text", new JsonProperty(context.getString(R.string.main_exit_btn_exit)));
        btnParams.add(bp);
        bp = new JsonObject();
        bp.setProperty("id", new JsonProperty(R.id.btn_mid));
        bp.setProperty("action", new JsonProperty(Dialog.BUTTON_NEGATIVE));
        bp.setProperty("text", new JsonProperty(context.getString(R.string.btn_cancel)));
        btnParams.add(bp);
        bp = new JsonObject();
        bp.setProperty("id", new JsonProperty(R.id.btn_right));
        bp.setProperty("action", new JsonProperty(Dialog.BUTTON_POSITIVE));
        bp.setProperty("text", new JsonProperty(context.getString(R.string.main_exit_btn_ok)));
        btnParams.add(bp);
        showAlertDialog(context, title, msg, btnParams, listener);
    }

    public static void showAlertDialog(Context context, String title, String msg, JsonProperty btnParams, final DialogInterface.OnClickListener listener) {
        if (context instanceof Activity && !((Activity)context).isFinishing()) {
            final AlertDialog dialog = new AlertDialog.Builder(context).create();
            dialog.show();
            dialog.setCancelable(false);
            Window window = dialog.getWindow();
            int index = 0;
            if (btnParams != null && btnParams.getType() == JsonPropertyType.Array) {
                if (btnParams.getCount() > 2) {
                    window.setContentView(R.layout.alert_dialog_3button_layout);
                } else {
                    window.setContentView(R.layout.alert_dialog_layout);
                }
                index = btnParams.getCount();
            } else {
                window.setContentView(R.layout.alert_dialog_layout);
            }
            DialogManager.setDialogAnimation(dialog);

            TextView tv_title = (TextView) window.findViewById(R.id.tv_title);
            TextView tv_text = (TextView) window.findViewById(R.id.tv_text);
            tv_title.setText(title);
            tv_text.setText(msg);

            if (index == 0) {
                Button btn_left = (Button) window.findViewById(R.id.btn_left);
                Button btn_right = (Button) window.findViewById(R.id.btn_right);
                btn_left.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.hide();
                        if (listener != null) {
                            listener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                        }
                    }
                });
                btn_left.setText(R.string.btn_ok);
                btn_right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.hide();
                        listener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                    }
                });
                btn_right.setText(R.string.btn_cancel);
            } else {
                for (final JsonProperty jp : btnParams.getItems()) {
                    Button button = (Button) window.findViewById(jp.getProperty("id").getInteger());
                    button.setText(jp.getProperty("text").getValue());
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.hide();
                            listener.onClick(dialog, jp.getProperty("action").getInteger());
                        }
                    });
                }
            }
        }
    }
}
